package com.mqz.zebraEcommerce.commodity.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.mqz.zebraEcommerce.commodity.entity.AttrAttrGroupRelationEntity;
import com.mqz.zebraEcommerce.commodity.entity.AttrEntity;
import com.mqz.zebraEcommerce.commodity.service.AttrAttrGroupRelationService;
import com.mqz.zebraEcommerce.commodity.service.AttrService;
import com.mqz.zebraEcommerce.commodity.service.CategoryService;
import com.mqz.zebraEcommerce.commodity.vo.AttrGroupAndAttrsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.mqz.zebraEcommerce.commodity.entity.AttrGroupEntity;
import com.mqz.zebraEcommerce.commodity.service.AttrGroupService;
import com.mqz.zebraEcommerce.common.utils.PageUtils;
import com.mqz.zebraEcommerce.common.utils.R;

import javax.annotation.Resource;

/**
 * 商品属性分组
 *
 * @author mqz
 * @email 952905931@qq.com
 * @date 2025-03-02 16:48:14
 */
@RestController
@RequestMapping("commodity/attrGroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Resource
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 根据条件返回列表
     */
    @RequestMapping("/list/{categoryId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("categoryId") Long categoryId) {
        PageUtils page = attrGroupService.queryPage(params, categoryId);

        return R.ok().put("page", page);
    }

    /**
     * 根据条件返回关联列表
     */
    @RequestMapping("/{attrGroupId}/attr/validRelation")
    public R AttrRelationList(@RequestParam Map<String, Object> params, @PathVariable("attrGroupId") Long attrGroupId) {
        PageUtils page = attrService.queryValidRelationAttrPage(params, attrGroupId);

        return R.ok().put("page", page);
    }

    /**
     * 返回attrGroupAndAttrVOs
     */
    @RequestMapping("/{categoryId}/vo")
    public R getAttrGroupAndAttrVOs(@PathVariable("categoryId") Long categoryId) {

        List<AttrGroupAndAttrsVO> attrGroupAndAttrsVOs = attrGroupService.getAttrGroupAndAttrsVOByCategoryId(categoryId);

        return R.ok().put("data", attrGroupAndAttrsVOs);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        AttrGroupEntity attrGroup = attrGroupService.getById(id);
        Long categoryId = attrGroup.getCategoryId();
        Long[] cascadedCategoryId = categoryService.getCascadedCategoryId(categoryId);
        attrGroup.setCascadedCategoryId(cascadedCategoryId);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrgroup) {
        attrGroupService.save(attrgroup);

        return R.ok();
    }

    /**
     * 保存关联(可考虑在 relation controller 中实现该 api)
     */
    @Resource
    private AttrAttrGroupRelationService attrAttrGroupRelationService;

    @RequestMapping("/attr/relation")
    public R saveRelation(@RequestBody List<AttrAttrGroupRelationEntity> relationEntities) {
        attrAttrGroupRelationService.saveBatch(relationEntities);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrgroup) {
        attrGroupService.updateById(attrgroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        attrGroupService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @Resource
    private AttrService attrService;

    /**
     * 获取关联
     */
    @RequestMapping("/{attrGroupId}/attr/relation")
    public R getAttrRelation(@PathVariable("attrGroupId") Long attrGroupId) {
        List<AttrEntity> relationAttr = attrService.getRelationAttr(attrGroupId);

        return R.ok().put("data", relationAttr);
    }

    /**
     * 删除关联
     */
    @RequestMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrAttrGroupRelationEntity[] attrAttrGroupRelationEntities) {
        attrService.deleteRelation(attrAttrGroupRelationEntities);

        return R.ok();
    }
}
